let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
    follow_index:0,
    level_index:0,
    next_follow_index:0,
    number:9,
    upImgs:[],
    sex_index:0,
    sex:['请选择','男','女'],
    isChange_index:0,
    cate1_index:0,
    cate2_index:0,
    cate3_index:0,
    payType_index:0,
    orderType_index:0,
    date:'请选择订车时间',
    deal_date:'请选择交车时间',
    city:'请选择上牌城市',
    trade_index:0,
    job_index:0,
    marry_index:0,
    like_index:0,
    add:'家庭地址',
    showMore:0,
    plateType_index:0


        
      
    },  

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        var id = options.id;
       // this.getCustomer(id);
        that.setData({
            platform:app.globalData.platform,    
            id:id,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack(){
        console.log('go back')
        wx.navigateBack(-1)
      },
      goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){
    
        }else{
            if(url=='index'){
                wx.reLaunch({
                  url: url,
                })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
        
        }
        console.log(url);
    },

      showModals:function(msg,url,index){
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'系统提示',
          content:msg,
          //showCancel:false,
          success:function(e){
           
                if(url=='#'){

                }else{
                    if(index=="#"){
                        wx.navigateTo({
                          url: url,
                        })
                   }
                   if(index=="index"){
                    wx.reLaunch({
                      url: url,
                    })
               }
                }          
                         
          }
          

        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getOrderSelect();
        this.getOrderById();
    },
    getOrderSelect(){
        let that = this;
        var userInfo=wx.getStorageSync('userInfo');
        var id = this.data.id
       wx.request({
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
         data: {
           //user_id:userInfo.id,
           //mobile:userInfo.phone,
           id:id
           //phone:userInfo.phone
         
             
         },
         //method: 'POST',
         dataType: 'json',
         timeout: 30000,
         url: app.globalData.domain + 'customer/getOrderSelect',
         success: (res) => {
          let data = res.data;
          console.log(data);
         var cate = data.cate;
         var cate1 = [];
         var cate1_array = [];
         cate1.push('请选择');
         cate1_array.push('');
         cate.forEach(ele=>{
           if(ele.level == 1){
             cate1.push(ele.title)
             cate1_array.push(ele);
           }
         })
         var customer = data.customer;
         var cate1 = [];
         var cate1_array = [];
         cate1.push('请选择');
         var cates ={id:0,title:'请选择'};
         cate1_array.push(cates);
         cate.forEach(ele=>{
           if(ele.level == 1){
             cate1.push(ele.title)
             cate1_array.push(ele);
           }
         })
         var cate1_index = 0;
         cate1_array.forEach(function(ele,index){    
          //   console.log(ele.id) 
         //    console.log(ele)     
           if(ele.id == customer.cate1){
                console.log(ele.id);
                console.log(customer.cate1);
                cate1_index=index;
           
            }   

         });
         /*****cate2 */
         var cate2_index=0;
         var cate2 = [];
         var cate2_array = [];
         cate2.push('请选择');
         var cates ={id:0,title:'请选择'};
         cate2_array.push(cates);
         cate.forEach(ele=>{
            if(ele.pid == customer.cate1){
              cate2.push(ele.title)
              cate2_array.push(ele);
            }
          })
          cate2_array.forEach(function(ele,index){    
            //   console.log(ele.id) 
           //    console.log(ele)     
             if(ele.id == customer.cate2){
                  console.log(ele.id);
                  console.log(customer.cate2);
                  cate2_index=index;
             
              }   
  
           });

           /********cate 3 */
           if(Number.isInteger(customer.cate3)  && customer.cate3>0){
            var cate3_index=0;
            var cate3 = [];
            var cate3_array = [];
            cate3.push('请选择');
            var cates ={id:0,title:'请选择'};
            cate3_array.push(cates);
            cate.forEach(ele=>{
               if(ele.pid == customer.cate2){
                 cate3.push(ele.title)
                 cate3_array.push(ele);
               }
             })
             cate3_array.forEach(function(ele,index){    
               //   console.log(ele.id) 
              //    console.log(ele)     
                if(ele.id == customer.cate3){
                     console.log(ele.id);
                     console.log(customer.cate3);
                     cate3_index=index;
                
                 }   

     
              });
              that.setData({
                  cate3_select:1,
                  cate3:cate3,            
                  cate3_array:cate3_array,
                  cate3_index:cate3_index,
              })
           }
           /*******cate 3 end  */
           that.setData({
            customer:customer,
            cate:cate,
            cate1:cate1,            
            cate1_array:cate1_array,
            cate1_index:cate1_index,
            cate2_select:1,
            cate2:cate2,            
            cate2_array:cate2_array,
            cate2_index:cate2_index,
            payType:data.payType,
            isChange:data.isChange,
            orderType:data.orderType,
            trade:data.trade,
            marry:data.marry,
            job:data.job,
            like:data.like
           })
         }
       
        })
      },
    getOrderById(){
        let d = this.data;
        var id = d.id;
        let that = this;
        var userInfo =  wx.getStorageSync('userInfo');
        var data = [];
        data['user_id'] = userInfo.id;
        data['id'] = id;
        var url = 'customer/getCustomerOrder';

      app.request(url,data,'get').then(res=>{ 
        console.log('---customer ----lose to reject'); 
         console.log(res);
         var order = res.customer;
         that.setData({
            sex_index:order.sex,
            isChange_index:order.ischange,
            orderType_index:order.order_type,
            payType_index:order.pay_type,
            date:order.order_date,
            city:order.city,
            plateType:res.plateType,
             order:order
         })
         //app.showModals(res.msg,res.url,res.index);
         //that.getIndex();
      });
    },
      cate1Change(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          cate1_index:index,
        })
        console.log(index)
        var cate1_array = this.data.cate1_array;
        console.log(cate1_array);
        var cate1_id = cate1_array[index].id;
        console.log('cate1_id=>'+cate1_id);
        var cate = this.data.cate;
        var cate2 = [];
        var cate2_array = [];
        cate2.push('请选择');
        cate2_array.push('');
        cate.forEach(ele=>{
          if(ele.pid == cate1_id){
            cate2.push(ele.title);
            cate2_array.push(ele)
          }
        })
        this.setData({
          cate2:cate2,
          cate2_array:cate2_array,
          cate1_id:cate1_id,
          cate3_select:0,
          cate2_select:index
        })
    
      },
      cate2Change(e){
        console.log(e);
        var index = e.detail.value;       
        console.log(index)
        var cate2_array = this.data.cate2_array;
        console.log(cate2_array);
        
        var array =cate2_array[index];
        var cate2_id = array.id 
        var last = array.last;
        if(last == 1){
          this.setData({
            cate2_index:index,
            cate2_id:cate2_id,
            cate3_select:0
          })
        }else{
          console.log('cate2_id=>'+cate2_id);
          var cate = this.data.cate;
          var cate3 = [];
          var cate3_array = [];
          cate3.push('请选择');
          cate3_array.push('');
          cate.forEach(ele=>{
            if(ele.pid == cate2_id){
              cate3.push(ele.title);
              cate3_array.push(ele)
            }
          })
          this.setData({
            cate2_index:index,
            cate3:cate3,
            cate3_array:cate3_array,
            cate2_id:cate2_id,
            cate3_select:1
          })
      
        }
       
      },
      cate3Change(e){
        console.log(e);
        var index = e.detail.value;
       
        console.log(index)
        var cate3_array = this.data.cate3_array;
        console.log(cate3_array);
        
        var array =cate3_array[index];
        var cate3_id = array.id 
    
          this.setData({
            cate3_index:index,
            cate3_id:cate3_id,
          
          })
       
      },
      sexChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
          sex_index:index
        })
      },
      isChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            isChange_index:index
        })
      },

      orderTypeChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            orderType_index:index
        })
      },
      payTypeChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            payType_index:index
        })
      },
      plateTypeChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            plateType_index:index
        })
      },
      dateChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            date:index
        })
      },
      dealDateChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            deal_date:index
        })
      },
      cityChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        console.log(index);
        var city = index.toString();
        //var city = JSON.stringify(index);
        console.log(city);
        this.setData({
            city:city
        })
      },
      tradeChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        console.log(index)
        this.setData({
            trade_index:index
        })
      },
      jobChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            job_index:index
        })
      },
      likeChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            like_index:index
        })
      },
      marryChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            marry_index:index
        })
      },
      addsChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
            add:index
        })
      },
      onSubmit(e) {
        console.log(`数据：${JSON.stringify(e.detail.value)}`)
        let that = this;
        console.log(e);
        var data= this.data;
        var value = e.detail.value;
       // value.inDate = this.data.inDate;
        var user = wx.getStorageSync('userInfo');
        if(value.name.length ==''){
          app.showModals('客户姓名不能为空','#','#')
          return false;
        }
        const regex = /^[1]{1}\d{10}$/;
        //  return regex.test(str);
        if(value.phone.length != 11){
        app.showModals('手机号码为11位数字','#','#')
          return false;
        }else if(regex.test(value.phone)){
      
        }else{
            app.showModals('手机号码为11位数字','#','#')
          return false;
        }
       // var stock_index= data.stock_index;
        if(value.sex == 0){
          app.showModals('请选择客户性别','#','#')
         // console.log(stock_index);
          return false;
        }
        if(value.idcard == ''){
           // app.showModals('请输入客户身份证号码','#','#')
           // console.log(stock_index);
           // return false;
          }
        if(value.isChange == 0){
         //   客户姓名
            app.showModals('请选择客户是否换购','#','#')
            return false;
          }
       
      
       if(value.cate1 == 0){
        app.showModals('请选择车型','#','#')
          //console.log(stock_index);
          return false;
       }
       let cate3_select = this.data.cate3_select;
       if(value.cate2 == 0){
        app.showModals('请选择车型','#','#')
        //console.log(stock_index);
        return false;
     }else{
      
       if(cate3_select == 1){
        if(value.cate3 == 0){
            app.showModals('请选择车型','#','#')
          return false;
        }
       }
     }
     if(value.orderType == 0){
        app.showModals('请选择订单类型','#','#')
        //console.log(stock_index);
        return false;
      }
      if(value.payType == 0){
        app.showModals('请选择付款方式','#','#')
        //console.log(stock_index);
        return false;
      }
      if(value.deal_date == '请选择交车时间'){
        app.showModals('请选择交车时间','#','#')
        //console.log(stock_index);
        return false;
      }
      if(value.city == '请选择上牌城市'){
        app.showModals('请选择上牌城市','#','#')
        //console.log(stock_index);
        return false;
      }
      if(value.plate_type == '请选择'){
        app.showModals('请选择车牌类型','#','#')
        //console.log(stock_index);
        return false;
      }
       let cate1_array = this.data.cate1_array;
       var cate1 = cate1_array[value.cate1].id;
       let cate2_array = this.data.cate2_array;
       var cate2 = cate2_array[value.cate2].id;
    
       if(cate3_select == 1){
        let cate3_array = this.data.cate3_array;
        var cate3= cate3_array[value.cate3].id;
       }else{
        var  cate3 = 0;
       }
     
       if(value.car_front_price==""){
        app.showModals('请输入定金金额','#','#')
        // console.log(stock_index);
         return false;
       }
       if(value.car_naked_price == ''){
        app.showModals('请输入裸车价格','#','#')
       // console.log(stock_index);
        return false;
      }
    
      
        wx.request({
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          data: {
            mobile:user.phone,
            user_id:user.id,
            value:JSON.stringify(value),
            id:data.id
            
          },
          //method: 'POST',
          dataType: 'json',
          timeout: 30000,
          url: app.globalData.domain + 'customer/toBought',
          success: (res) => {
      
            console.log(res);
            var data = res.data;
      
           // var data = JSON.parse(data.input);
            console.log(data);
            if(res.statusCode == 200){
             // console.log(data.url)
              that.showModals(data.msg,data.url,data.index)
              
            }
          }
        
         })
      
      
      },
      showMore(){
          let show = this.data.showMore;
          this.setData({
              showMore:!show
          })
      },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})